package com.czl.module_work.view

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.cooltechworks.views.shimmer.ShimmerRecyclerView
import com.czl.base.callback.TimePickerListener
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.MembersBean
import com.czl.base.mvvm.ui.ContainerFmActivity
import com.czl.base.widget.TimePickerPopup
import com.czl.module_work.R
import com.czl.module_work.viewModel.MyOrderDetailViewModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import java.lang.StringBuilder
import java.util.*


/**
 * 创建日期：2021/12/28  16:23
 * 类说明:
 * @author：86152
 */
class OrderReceivingPop(context: Context) : BottomPopupView(context) {

    private lateinit var selectMemberView: TextView

    private lateinit var selectServiceTimeView: TextView

    private lateinit var remarkView: EditText

    private var mListener: OrderReceivingListener? = null

    override fun getImplLayoutId(): Int = R.layout.work_dialog_order_receiving

    private var mMember: MutableList<MembersBean> = mutableListOf()

    private var serviceTime: String = ""

    private var startDate: String = "1994-01-01 00:00:00"

    private var endDate: String = "2030-01-01 00:00:00"

    private var orderTypeId: String = ""

    fun setMember(member: List<MembersBean>) {
        this.mMember.clear()
        this.mMember.addAll(member)
        val names = StringBuilder()
        names.append("协助人:")
        var pos = 0;
        member.forEach {
            names.append(it.nickName)
            pos++
            if (pos < member.size) {
                names.append(",")
            }
        }
        selectMemberView.text = names
    }

    fun setOrderTypeId(orderTypeId: String) {
        this.orderTypeId = orderTypeId
    }

    fun setStartDate(date: String) {
        this.startDate = if (TextUtils.isEmpty(date)) "1994-01-01 00:00:00" else date
    }

    fun setEndDate(date: String) {
        this.endDate = if (TextUtils.isEmpty(date)) "2030-01-01 00:00:00" else date
    }

    fun setOrderReceivingListener(listener: OrderReceivingListener) {
        this.mListener = listener
    }

    override fun initPopupContent() {
        super.initPopupContent()
        selectMemberView = findViewById(R.id.tv_help_member)
        selectServiceTimeView = findViewById(R.id.tv_service_time)
        remarkView = findViewById(R.id.et_remark)
        findViewById<View>(R.id.tv_cancel).setOnClickListener { dismiss() }
        findViewById<View>(R.id.rl_help_member).setOnClickListener {
            val intent = Intent(context, ContainerFmActivity::class.java)
            intent.putExtra(
                ContainerFmActivity.FRAGMENT,
                AppConstants.Router.Work.F_WORK_MEMBERS_SELECT
            )
            intent.putExtra(ContainerFmActivity.BUNDLE, Bundle().apply {
                putBoolean(
                    "isSingle",
                    false
                )
            })
            ActivityUtils.startActivity(intent)
        }
        findViewById<View>(R.id.rl_service_time).setOnClickListener {

            val startCalendar = Calendar.getInstance()
            startCalendar.time = TimeUtils.string2Date(startDate)

            val endCalendar = Calendar.getInstance()
            endCalendar.time = TimeUtils.string2Date(endDate)
            val popup: TimePickerPopup =
                TimePickerPopup(context)
                    .setDateRange(startCalendar, endCalendar)
                    .setTimePickerListener(object : TimePickerListener {
                        override fun onTimeChanged(date: Date?) {
                            //时间改变
                        }

                        override fun onTimeConfirm(date: Date, view: View?) {
                            //点击确认时间
                            serviceTime = TimeUtils.date2String(date, "yyyy-MM-dd")
                            selectServiceTimeView.text =
                                "服务时间:" + serviceTime
                        }
                    })

            XPopup.Builder(context)
                .asCustom(popup)
                .show()
        }
        findViewById<View>(R.id.tv_confirm).setOnClickListener {
            if (TextUtils.isEmpty(serviceTime)) {
                Toast.makeText(context.applicationContext, "选择服务时间!", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val note = remarkView.text.toString()
            mListener?.confirm(mMember, note, serviceTime)
            dismiss()
        }

    }

    interface OrderReceivingListener {

        fun confirm(member: MutableList<MembersBean>, note: String, serviceTime: String)

    }
}
